package com.maxsalad.uc.dao;

import com.maxsalad.common.dao.BaseDao;
import com.maxsalad.uc.pojo.TOrderDetailEntity;
import com.maxsalad.uc.pojo.TOrderEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-17.
 */
@Repository
public class OrderDetailDao extends BaseDao<TOrderDetailEntity> {
    private void baseSearchParam(TOrderDetailEntity orderDetailEntity, StringBuffer hql) {
        if (orderDetailEntity == null) {
            return;
        }
        if (orderDetailEntity.getId() != 0) {
            hql.append(" and t.id =:id");
        }
        //orderId
        if (orderDetailEntity.getOrderId() != 0) {
            hql.append(" and t.orderId =:orderId");
        }
        //dishId
        if (orderDetailEntity.getDishId() != 0) {
            hql.append(" and t.dishId =:dishId");
        }
        //quantity
        if (orderDetailEntity.getQuantity() != 0) {
            hql.append(" and t.quantity =:quantity");
        }
    }

    /**
     * 查询所有平台信息
     *
     * @param orderDetailEntity
     * @return
     * @throws Exception
     */
    public Map queryAllOrderDetail(TOrderDetailEntity orderDetailEntity) throws Exception {
        StringBuffer hql = new StringBuffer("from TOrderDetailEntity t where 1=1 ");
        this.baseSearchParam(orderDetailEntity, hql);
        hql.append(" order by t.orderOrder asc ");
        return this.pageQueryByHql(hql.toString(), orderDetailEntity);
    }

    /**
     * 不分页查询
     *
     * @param orderDetailEntity
     * @return
     * @throws Exception
     */
    public List<TOrderEntity> queryAllOrderDetailNoPage(TOrderDetailEntity orderDetailEntity) throws Exception {
        StringBuffer hql = new StringBuffer("from TOrderDetailEntity t where 1=1 ");
        this.baseSearchParam(orderDetailEntity, hql);
        hql.append(" order by t.createTime desc ");
        return this.findByHql(hql.toString(), orderDetailEntity);
    }


    public List<TOrderDetailEntity> queryOrderDetailById(Long[] orderIds) throws Exception {
        Map paramMap = new HashMap();
        paramMap.put("orderIds", orderIds);
        paramMap.put("pageSize", orderIds.length);

        StringBuffer hql = new StringBuffer("from TOrderDetailEntity t where 1=1 ");
        hql.append(" and t.orderId in :orderIds");
        hql.append(" order by t.createTime asc ");
        return this.pageQuery(hql.toString(), paramMap, false);
    }

}